<!-- index.vue -->
<template>
  <view class="container">
    <!-- 顶部统计 -->
    <view class="header">
      <view class="stats">
        <text>共 {{ total }} 节课</text>
        <text class="attended">已点名：{{ attended }}</text>
        <text class="unattended">未点名：{{ unattended }}</text>
      </view>
    </view>

    <!-- 日历 -->
    <view class="calendar">
      <view class="cal-header">
        <!-- 左侧：上个月按钮 -->
        <button class="nav prev" @tap="goPrev"> 
          <text class="nav-icon">‹</text>
        </button>
        
        <!-- 中间：月份和展开按钮 -->
        <view class="month-section">
          <text class="month">{{ year }}年 {{ month + 1 }}月</text>
          <button class="expand-btn" @tap="toggleDatePicker">
            <text class="expand-icon">{{ showDatePicker ? '▲' : '▼' }}</text>
          </button>
        </view>

        <!-- 右侧：下个月按钮和今天按钮 -->
        <view class="right-section">
          <button class="nav next" @tap="goNext">
            <text class="nav-icon">›</text>
          </button>
          <button class="today-btn" @tap="backToday">今天</button>
        </view>
      </view>
      
      <!-- 日期选择器 -->
      <view v-if="showDatePicker" class="date-picker">
        <view class="picker-row">
          <text class="picker-label">年份:</text>
          <picker class="year-picker" :value="yearIndex" :range="years" @change="onYearChange">
            <view class="picker-value">{{ year }}</view>
          </picker>
        </view>
        <view class="picker-row">
          <text class="picker-label">月份:</text>
          <picker class="month-picker" :value="month" :range="months" @change="onMonthChange">
            <view class="picker-value">{{ month + 1 }}月</view>
          </picker>
        </view>
        <view class="picker-row">
          <text class="picker-label">日期:</text>
          <picker class="day-picker" :value="dayIndex" :range="days" @change="onDayChange">
            <view class="picker-value">{{ selectedDate.getDate() }}日</view>
          </picker>
        </view>
      </view>

      <view class="week-days">
        <text v-for="d in weekDay" :key="d" class="week-day">{{ d }}</text>
      </view>
      <view :class="['dates', isWeekView ? 'week-view' : 'month-view']">
        <view
          v-for="(item, i) in renderDays"
          :key="i"
          :class="[
            'day',
            { today: item.isToday },
            { selected: item.selected },
            { 'has-courses': item.hasCourses },
            { 'other-month': !item.curMonth }
          ]"
          @tap="selectDay(item)"
        >
          <text class="day-text">{{ item.day }}</text>
          <view v-if="item.hasCourses" class="course-indicator"></view>
        </view>
      </view>
      <view class="cal-footer">
        <button class="toggle" @tap="toggleView">
          {{ isWeekView ? '▽' : '△' }}
        </button>
      </view>
    </view>

    <!-- 当日课表 -->
    <view class="course-area">
      <text class="course-title">{{ titleDate }}课表</text>
      <view v-if="courseDateList.length === 0" class="empty">该日期没有安排课程</view>
      <view v-for="item in courseDateList" :key="item.id" class="course-card">
        <view class="course-header">
          <text class="name">{{ item.name }}</text>
          <text class="time">{{ item.startTime }}-{{ item.endTime }}</text>
        </view>
        <view class="info">
          <text>老师：{{ item.coachM.name }}</text>
          <text>教室：{{ item.site }}</text>
        </view>
        <view class="attend-line">
          <view v-if="item.coachListS.length === 0">助教：无</view>
          <view v-else>助教：{{ item.coachListS.map(row => row.name).join(',') }}</view>
		  <view>
			  <button v-if="item.state === 1" class="call-btn">去点名</button>
			  <button v-else-if="item.state === 0" class="no-call-btn">未开始</button>
			  <button v-else class="no-call-btn">已点名</button>
		  </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getDateCourse } from '@/api/schedule.js'

/* ===== 数据区 ===== */
const today = new Date()
const year = ref(today.getFullYear())
const month = ref(today.getMonth())
const date = ref(today.getDate())
const isWeekView = ref(true)
const selectedDate = ref(new Date())
const showDatePicker = ref(false)

// 星期表头
const weekDay = ['一', '二', '三', '四', '五', '六', '日']

//后端获取的课程日期
const courseDateList = ref([])

function requestCourse() {
	getDateCourse(formatDate(selectedDate.value)).then(res => {
		  courseDateList.value = res.data
	})
}

onMounted(() => {
	requestCourse()
})

/* ===== 计算区 ===== */
const years = computed(() => {
  const currentYear = new Date().getFullYear()
  return Array.from({ length: 10 }, (_, i) => currentYear - 5 + i)
})

const months = computed(() => {
  return Array.from({ length: 12 }, (_, i) => `${i + 1}月`)
})

const days = computed(() => {
  const daysInMonth = new Date(year.value, month.value + 1, 0).getDate()
  return Array.from({ length: daysInMonth }, (_, i) => `${i + 1}日`)
})

const yearIndex = computed(() => {
  return years.value.findIndex(y => y === year.value)
})

const dayIndex = computed(() => {
  return selectedDate.value.getDate() - 1
})

const renderDays = computed(() => {
  const y = year.value
  const m = month.value
  const first = new Date(y, m, 1)
  const last = new Date(y, m + 1, 0)
  const firstDay = first.getDay() === 0 ? 7 : first.getDay()
  const days = last.getDate()
  const arr = []


  if (isWeekView.value) {
    // 周视图：选中日期所在周
    const sel = selectedDate.value
    const dayOfWeek = sel.getDay() === 0 ? 7 : sel.getDay()
    const start = new Date(sel)
    start.setDate(sel.getDate() - dayOfWeek + 1)
    for (let i = 0; i < 7; i++) {
      const d = new Date(start)
      d.setDate(start.getDate() + i)
      arr.push(makeDayItem(d, true))
    }
  } else {
    // 月视图
    // 上月尾部
    const prevLast = new Date(y, m, 0).getDate()
    for (let i = firstDay - 1; i > 0; i--) {
      const d = new Date(y, m - 1, prevLast - i + 1)
      arr.push(makeDayItem(d, false))
    }
    // 当月
    for (let i = 1; i <= days; i++) {
      const d = new Date(y, m, i)
      arr.push(makeDayItem(d, true))
    }
    // 下月头部
    const tail = 42 - arr.length
    for (let i = 1; i <= tail; i++) {
      const d = new Date(y, m + 1, i)
      arr.push(makeDayItem(d, false))
    }
  }
  return arr
})


const attended = computed(() => '0节')

const titleDate = computed(() => {
  const d = selectedDate.value
  const isToday = d.toDateString() === today.toDateString()
  return isToday ? `今日 (${d.getMonth() + 1}月${d.getDate()}日)` : `${d.getMonth() + 1}月${d.getDate()}日`
})

/* ===== 方法区 ===== */
function formatDate(d) {
  const y = d.getFullYear()
  const m = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  return `${y}-${m}-${day}`
}
function addDay(d, n) {
  const t = new Date(d)
  t.setDate(t.getDate() + n)
  return t
}
function makeDayItem(d, cur = true) {
  return {
    day: d.getDate(),
    curMonth: cur,
    isToday: d.toDateString() === today.toDateString(),
    selected: d.toDateString() === selectedDate.value.toDateString(),
    fullDate: new Date(d)
  }
}

//选择日期后触发的方法
function selectDay(item) {
  if (!item.curMonth) return
  selectedDate.value = new Date(item.fullDate)
	console.log("我选中的日期：", formatDate(selectedDate.value))
  year.value = selectedDate.value.getFullYear()
  month.value = selectedDate.value.getMonth()
	requestCourse()
}
function goPrev() {
  if (isWeekView.value) {
    selectedDate.value = addDay(selectedDate.value, -7)
  } else {
    selectedDate.value = new Date(year.value, month.value - 1, 1)
  }
  year.value = selectedDate.value.getFullYear()
  month.value = selectedDate.value.getMonth()
  requestCourse()
}
function goNext() {
  if (isWeekView.value) {
    selectedDate.value = addDay(selectedDate.value, 7)
  } else {
    selectedDate.value = new Date(year.value, month.value + 1, 1)
  }
  year.value = selectedDate.value.getFullYear()
  month.value = selectedDate.value.getMonth()
  requestCourse()
}
function toggleView() {
  isWeekView.value = !isWeekView.value
}
//返回今天日期
function backToday() {
  selectedDate.value = new Date()
  year.value = today.getFullYear()
  month.value = today.getMonth()
  showDatePicker.value = false
  requestCourse()
}
function toggleDatePicker() {
  showDatePicker.value = !showDatePicker.value
}
function onYearChange(e) {
  const index = parseInt(e.detail.value)
  year.value = years.value[index]
  updateSelectedDate()
}
function onMonthChange(e) {
  month.value = parseInt(e.detail.value)
  updateSelectedDate()
}
function onDayChange(e) {
  const day = parseInt(e.detail.value) + 1
  selectedDate.value = new Date(year.value, month.value, day)
  requestCourse()
}
function updateSelectedDate() {
  // 确保日期在有效范围内
  const daysInMonth = new Date(year.value, month.value + 1, 0).getDate()
  const currentDay = Math.min(selectedDate.value.getDate(), daysInMonth)
  selectedDate.value = new Date(year.value, month.value, currentDay)
  requestCourse()
}
</script>

<style>
/* 容器 */
.container {
  padding: 20rpx;
  background: #f5f7fa;
  min-height: 100vh;
}
/* 头部 */
.header {
  text-align: center;
  margin-bottom: 20rpx;
}
.stats {
  display: flex;
  justify-content: space-around;
  background: #ffe8e3;
  padding: 12rpx 0;
  border-radius: 8rpx;
  margin-top: 10rpx;
  font-size: 26rpx;
}
.attended {
  color: #27ae60;
}
.unattended {
  color: #e74c3c;
}

/* 日历样式优化 */
.calendar {
  background: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
}

/* 日历头部 - 重新设计 */
.cal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
  padding: 0 10rpx;
  gap: 20rpx;
}

/* 左侧导航按钮 */
.nav {
  width: 70rpx;
  height: 70rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: none;
  background: #f8f9fa;
  color: #ff7e5f;
  font-size: 24rpx;
  transition: all 0.2s;
  flex-shrink: 0;
}

.nav:active {
  background: #ffede4;
  transform: scale(0.95);
}

.nav-icon {
  font-size: 36rpx;
  font-weight: bold;
  line-height: 1;
}

/* 中间月份区域 */
/* 2025年10月 */
.month-section {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  min-width: 0;
}

.month {
  font-size: 34rpx;
  font-weight: 600;
  padding-left: 10%;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  margin-right: 20rpx;
}

/* 展开按钮 */
.expand-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1rpx solid #ff7e5f;
  background: #ffede4;
  color: #ff7e5f;
  transition: all 0.2s;
  flex-shrink: 0;
}

.expand-btn:active {
  background: #ffddd0;
  transform: scale(0.95);
}

.expand-icon {
  font-size: 20rpx;
  font-weight: bold;
}

/* 右侧区域 */
.right-section {
  display: flex;
  align-items: center;
  gap: 15rpx;
  flex-shrink: 0;
}

/* 今天按钮 */
.today-btn {
  font-size: 26rpx;
  padding: 16rpx 24rpx;
  border-radius: 35rpx;
  border: 1rpx solid #ff7e5f;
  background: #ffede4;
  color: #ff7e5f;
  transition: all 0.2s;
  white-space: nowrap;
  font-weight: 500;
}

.today-btn:active {
  background: #ffddd0;
  transform: scale(0.95);
}

/* 日期选择器 */
.date-picker {
  background: #f8f9fa;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  border: 1rpx solid #e9ecef;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.04);
}

.picker-row {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
}

.picker-label {
  font-size: 28rpx;
  color: #495057;
  width: 120rpx;
}

.picker-value {
  font-size: 28rpx;
  color: #ff7e5f;
  padding: 15rpx 25rpx;
  background: white;
  border-radius: 10rpx;
  border: 1rpx solid #dee2e6;
  flex: 1;
  text-align: center;
  transition: all 0.2s;
}

.picker-value:active {
  background: #fff5f2;
}

.year-picker,
.month-picker,
.day-picker {
  flex: 1;
}

/* 星期表头 */
.week-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-size: 26rpx;
  color: #7f8c8d;
  margin-bottom: 20rpx;
  padding: 0 10rpx;
}

.week-day {
  padding: 15rpx 0;
  font-weight: 500;
}

/* 日期网格 */
.dates {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  gap: 8rpx;
  padding: 0 10rpx;
}

.day {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 80rpx;
  border-radius: 16rpx;
  margin: 4rpx;
  transition: all 0.2s;
  overflow: hidden;
}

.day:active {
  transform: scale(0.95);
}

.day-text {
  font-size: 28rpx;
  font-weight: 500;
  z-index: 1;
}

.day.other-month {
  color: #d1d5db;
}

.day.other-month .day-text {
  color: #d1d5db;
}

.day.today {
  background: linear-gradient(135deg, #ff7e5f, #ff9e87);
  color: #fff;
  box-shadow: 0 4rpx 12rpx rgba(255, 126, 95, 0.3);
}

.day.selected {
  background: linear-gradient(135deg, #ff9e87, #ffb7a6);
  color: #fff;
  box-shadow: 0 4rpx 12rpx rgba(255, 158, 135, 0.3);
}

.day.has-courses .course-indicator {
  position: absolute;
  bottom: 8rpx;
  width: 8rpx;
  height: 8rpx;
  background: #3498db;
  border-radius: 50%;
  z-index: 1;
}

.day.today .course-indicator,
.day.selected .course-indicator {
  background: #fff;
}

/* 日历底部 */
.cal-footer {
  display: flex;
  justify-content: center;
  margin-top: 30rpx;
}

.toggle {
  font-size: 26rpx;
  color: #ff7e5f;
  border: none;
  background: none;
  padding: 15rpx 30rpx;
  border-radius: 30rpx;
  transition: all 0.2s;
}

.toggle:active {
  background: #ffede4;
  transform: scale(0.95);
}

/* 课表 */
.course-area {
  background: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
}

.course-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
  display: block;
  padding-left: 20rpx;
  border-left: 6rpx solid #ff7e5f;
}

.empty {
  text-align: center;
  color: #95a5a6;
  padding: 60rpx 0;
  font-size: 28rpx;
}

.course-card {
  margin-bottom: 30rpx;
  padding: 30rpx;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
  border: 1rpx solid #f1f3f4;
  transition: all 0.2s;
}

.course-card:active {
  transform: translateY(2rpx);
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.08);
}

/* 第一行 */
.course-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20rpx;
}

.name {
  color: #ff7e5f;
  font-size: 32rpx;
  font-weight: bold;
}

.time {
  color: #7f8c8d;
  font-size: 26rpx;
}

/* 第二行 */
.info {
  display: flex;
  justify-content: space-between;
  font-size: 26rpx;
  color: #495057;
  margin-bottom: 20rpx;
}

/* 第三行 */
.attend-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  margin-top: 30rpx;
  margin-bottom: 10rpx;
  font-size: 28rpx; /* 可选：统一字号 */
}

/* 去点名 按钮 */
.call-btn {
  background: #fee9da;
  color: #fe8b64;
  border: none;
  padding: 12rpx 30rpx;
  border-radius: 30rpx;
  font-size: 24rpx;
  font-weight: 540;
  transition: all 0.2s;
}
/*未点名 */
.no-call-btn {
    background: #b2b6bf;
    color: #ffffff;
  border: none;
  padding: 12rpx 30rpx;
  border-radius: 30rpx;
  font-size: 24rpx;
  font-weight: 540;
  transition: all 0.2s;
}

.call-btn:active {
  background: #ffddd0;
  transform: scale(0.95);
}

.call-btn.disabled {
  opacity: 0.5;
}
</style>